package team.seekers.oj.lesson.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.oj.common.annotation.UserSession;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.lesson.dto.ChapterCreateReqDTO;
import team.seekers.oj.lesson.service.ChapterManageService;

import javax.validation.Valid;

@Controller
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterManageService chapterManageService;


    @PostMapping("/create")
    @ApiResponseBody
    public Long create(@RequestBody @Valid ChapterCreateReqDTO reqDTO,
                       @UserSession UserSessionDTO userSessionDTO) {
        // 增补
        reqDTO.setUserId(userSessionDTO.getUserId());
        return chapterManageService.create(reqDTO);
    }

    @GetMapping("/delete/{chapterId}")
    @ApiResponseBody
    public Void delete(@PathVariable("chapterId") Long chapterId,
                       @UserSession UserSessionDTO userSessionDTO) {
        // 增补
        chapterManageService.isChapterManager(chapterId,userSessionDTO);
        chapterManageService.delete(chapterId);
        return null;
    }

}
